package com.mnb.base.service.impl;

import com.mnb.base.entity.StRiverR;
import com.mnb.common.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.mnb.base.entity.StPptnR;

import java.util.*;

import com.mnb.base.mapper.StPptnRMapper;
import com.mnb.base.service.StPptnRService;

@Service
public class StPptnRServiceImpl implements StPptnRService {

    @Resource
    private StPptnRMapper stPptnRMapper;


    @Override
    public R getList(StPptnR stPptnR) {
        List<StPptnR> list = stPptnRMapper.getList(stPptnR);
        return R.ok(list);
    }

    @Override
    public R insert(StPptnR stPptnR) {
        StPptnR count = stPptnRMapper.selectByPrimaryKey(stPptnR.getStcd(), stPptnR.getTm());
        if (count != null) {
            return R.fail("数据已存在");
        }
        stPptnRMapper.insert(stPptnR);
        return R.ok();
    }

    @Override
    public R update(StPptnR stPptnR) {
        stPptnRMapper.updateByPrimaryKey(stPptnR);
        return R.ok();
    }

    @Override
    public R delete(List<StPptnR> list) {
        for (StPptnR stPptnR : list) {
            stPptnRMapper.deleteByPrimaryKey(stPptnR.getStcd(), stPptnR.getTm());
        }
        return R.ok();
    }

    @Override
    public R getChartData(StPptnR stPptnR) {
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();

        // 设置 etm 为今天 8 点
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date etm = calendar.getTime();

        // 设置 btm 为昨天 8 点（在 etm 基础上减去 1 天）
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date btm = calendar.getTime();
        stPptnR.setBtm(btm);
        stPptnR.setEtm(etm);
        List<StPptnR> list = stPptnRMapper.getChartData(stPptnR);
        return R.ok(list);
    }
}
